package com.tsd.wxpay.utils;

import com.alibaba.fastjson.JSON;
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.util.SignUtils;
import com.tsd.common.vo.WxPayParamsVo;
import com.tsd.core.utils.HttpUtil;
import com.tsd.wxpay.entity.WeixinPayNotifyLogExt;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: WxPayUtils
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2022/7/12 16:44
 * @Version: 1.0
 */
public class WxPayUtils {

    public static WxPayUnifiedOrderRequest createRequest(WxPayParamsVo paramsVo, HttpServletRequest request) {
        WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
        orderRequest.setOutTradeNo(paramsVo.getOut_trade_no());
        orderRequest.setOpenid(paramsVo.getOpen_id());
        orderRequest.setBody(paramsVo.getBody());
        if (paramsVo.getAttach() != null) {
            orderRequest.setAttach(JSON.toJSONString(paramsVo.getAttach()));
        }
        orderRequest.setTotalFee(paramsVo.getTotal_fee());
        orderRequest.setSpbillCreateIp(HttpUtil.getClientIP(request));
        return orderRequest;
    }

    public static WxPayUnifiedOrderRequest createRequest(String outTradeNo, String openId, String body, Map<String, Object> attach,
                                                         int totalFee, HttpServletRequest request) {
        WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
        orderRequest.setOutTradeNo(outTradeNo);
        orderRequest.setOpenid(openId);
        orderRequest.setBody(body);
        orderRequest.setAttach(JSON.toJSONString(attach));
        orderRequest.setTotalFee(totalFee);
        orderRequest.setSpbillCreateIp(HttpUtil.getClientIP(request));
        return orderRequest;
    }

    public static WxPayUnifiedOrderV3Request createV3Request(String outTradeNo, String openId, String description,
                                                             Map<String, Object> attach, int total, HttpServletRequest request) {
        WxPayUnifiedOrderV3Request v3Request = new WxPayUnifiedOrderV3Request();
        v3Request.setOutTradeNo(outTradeNo);
        WxPayUnifiedOrderV3Request.Payer payer = new WxPayUnifiedOrderV3Request.Payer();
        payer.setOpenid(openId);
        v3Request.setPayer(payer);
        v3Request.setAttach(JSON.toJSONString(attach));
        v3Request.setDescription(description);
        WxPayUnifiedOrderV3Request.Amount amount = new WxPayUnifiedOrderV3Request.Amount();
        amount.setTotal(total);
        v3Request.setAmount(amount);
        WxPayUnifiedOrderV3Request.SceneInfo sceneInfo = new WxPayUnifiedOrderV3Request.SceneInfo();
        sceneInfo.setPayerClientIp(HttpUtil.getClientIP(request));
        v3Request.setSceneInfo(sceneInfo);
        return v3Request;
    }

    public static WxPayMpOrderResult createJsApiSign(WeixinPayNotifyLogExt logExt, WxPayConfig config) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        WxPayMpOrderResult payResult = WxPayMpOrderResult.builder()
                .appId(logExt.getApp_id())
                .timeStamp(timestamp)
                .nonceStr(logExt.getNonce_str())
                .packageValue("prepay_id=" + logExt.getPrepay_id())
                .signType(config.getSignType())
                .build();
        payResult.setPaySign(SignUtils.createSign(payResult, config.getSignType(), config.getMchKey(), null));
        return payResult;
    }

    public static WxPayAppOrderResult createAppSign(WeixinPayNotifyLogExt logExt, WxPayConfig config) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        Map<String, String> configMap = new HashMap<>(8);
        configMap.put("prepayid", logExt.getPrepay_id());
        configMap.put("partnerid", logExt.getMch_id());
        String packageValue = "Sign=WXPay";
        configMap.put("package", packageValue);
        configMap.put("timestamp", timestamp);
        configMap.put("noncestr", logExt.getNonce_str());
        configMap.put("appid", logExt.getApp_id());
        return WxPayAppOrderResult.builder()
                .sign(SignUtils.createSign(configMap, config.getSignType(), config.getMchKey(), null))
                .prepayId(logExt.getPrepay_id())
                .partnerId(logExt.getMch_id())
                .appId(logExt.getApp_id())
                .packageValue(packageValue)
                .timeStamp(timestamp)
                .nonceStr(logExt.getNonce_str())
                .build();
    }
}
